﻿Public Class REVENUEID
    Private connect As CONDB = CONDB.NewConnection()
    Private sC As FILTERCLASS
    Dim s As FILTERCLASSGRID
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub REVENUEID_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridFinchcode()
        SelectDatagridFinrecode()

        FINCHCODEGrid.PrimaryGrid.SetSort(FINCHCODEGrid.PrimaryGrid.Columns(2), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)
        FINRECODEGrid.PrimaryGrid.SetSort(FINRECODEGrid.PrimaryGrid.Columns(3), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)

        DesignForm(Me)
        DesignGrid(FINCHCODEGrid)
        DesignGrid(FINRECODEGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub cleardata()
        CCODETxt.Text = ""
        NAMETxt.Text = ""
        finchcodeStatusCheckbox.Checked = False
        NAMERECODETxt.Text = ""
        'CCODESEARCH.Text = ""
    End Sub

    '-----------C Code------------------
    Private Sub SelectDatagridFinchcode()
        Dim objSelectDataFINCHCODE As New FINCHCODE
        Dim FINCHCODE As DataTable
        FINCHCODE = connect.GetTable(objSelectDataFINCHCODE.SelectFinchcode_)
        FINCHCODEGrid.PrimaryGrid.DataSource = FINCHCODE
    End Sub

    Private Sub FINCHCODEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles FINCHCODEGrid.CellClick
        Dim Sql As String
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            CCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            CCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value() IsNot DBNull.Value Then
            NAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value()
        Else
            NAMETxt.Text = ""
        End If
        'If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value() IsNot DBNull.Value Then
        '    CCODESEARCH.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value()
        'Else
        '    CCODESEARCH.Text = ""
        'End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            NAMETxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            NAMETxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            finchcodeStatusCheckbox.Checked = True
        Else
            finchcodeStatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                'Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE finchcode SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `ccode` = '" & CCODETxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try

            sC.SetSql = "SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1"
        End If


        Dim dt As New DataTable
        Sql = "SELECT  `rcode` as 'รหัส' ,finchcode.`name` as 'รหัสรายได้' ,finrecode.`name` as 'รหัสรายรับทางการเงิน' ,finrecode.`status` as 'สถานะการเปิดใช้งาน' "
        Sql += "FROM finrecode  "
        Sql += "LEFT JOIN (SELECT * FROM finchcode WHERE `status` = 1) as finchcode ON finrecode.`ccode` = finchcode.`ccode` "
        Sql += "WHERE finchcode.`ccode` = '" & Convert.ToString(NAMETxt.Tag).Trim & "' "
        dt = connect.GetTable(Sql)
        FINRECODEGrid.PrimaryGrid.DataSource = dt
        connect.Dispose()
    End Sub

    Private Sub addFinchcode_Click(sender As Object, e As EventArgs) Handles addFinchcode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If NAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New FINCHCODE
                    objInsertData.NAME_ = NAMETxt.Text
                    objInsertData.STATUS_ = finchcodeStatusCheckbox.Checked
                    objInsertData.insertFinchcodeData()

                    SelectDatagridFinchcode()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editFinchcode_Click(sender As Object, e As EventArgs) Handles editFinchcode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                If editStat = 1 Then
                    Dim objeditData As New FINCHCODE
                    objeditData.CCODE_ = CCODETxt.Text
                    objeditData.NAME_ = NAMETxt.Text
                    objeditData.STATUS_ = finchcodeStatusCheckbox.Checked
                    objeditData.editMashosp()

                    SelectDatagridFinchcode()

                    cleardata()

                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnfinchcode_Click(sender As Object, e As EventArgs) Handles btnfinchcode.Click
        Dim nextform As New IMPORTEXCEL(finchcode.Text)
        nextform.ShowDialog()
    End Sub

    '------------R Code------------------
    Private Sub SelectDatagridFinrecode()
        Dim objSelectDataFINRECODE As New FINRECODE
        Dim FINRECODE As DataTable
        FINRECODE = connect.GetTable(objSelectDataFINRECODE.SelectFinrecode_)
        FINRECODEGrid.PrimaryGrid.DataSource = FINRECODE
    End Sub


    Private Sub FINRECODEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles FINRECODEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            RECODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            RECODETxt.Text = ""
        End If
        'If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value() IsNot DBNull.Value Then
        '    CCODESEARCH.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value()
        'Else
        '    CCODESEARCH.Text = ""
        'End If
        'If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
        '    CCODESEARCH.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        'Else
        '    CCODESEARCH.Tag = ""
        'End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value() IsNot DBNull.Value Then
            NAMERECODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value()
        Else
            NAMERECODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            finrecodestatuscheck.Checked = True
        Else
            finrecodestatuscheck.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE finrecode SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `rcode` = '" & RECODETxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
    End Sub

    Private Sub addFinrecode_Click(sender As Object, e As EventArgs) Handles addFinrecode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If NAMERECODETxt.Text.ToString.Length > 0 And NAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New FINRECODE
                    objInsertData.NAMER_ = NAMERECODETxt.Text
                    objInsertData.CCODE_ = Convert.ToString(NAMETxt.Tag).Trim
                    objInsertData.STATUS_ = finrecodestatuscheck.Checked
                    objInsertData.insertFinrecodeData()

                    SelectDatagridFinrecode()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editFinrecode_Click(sender As Object, e As EventArgs) Handles editFinrecode.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                If editStat = 1 Then
                    Dim objeditData As New FINRECODE
                    objeditData.RCODE_ = RECODETxt.Text
                    objeditData.NAMER_ = NAMERECODETxt.Text
                    objeditData.CCODE_ = Convert.ToString(NAMETxt.Tag).Trim
                    objeditData.STATUS_ = finchcodeStatusCheckbox.Checked
                    objeditData.editFinrecodeData()

                    SelectDatagridFinrecode()

                    cleardata()

                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnfinrecode_Click(sender As Object, e As EventArgs) Handles btnfinrecode.Click
        Dim nextform As New IMPORTEXCEL(finrecode.Text)
        nextform.ShowDialog()
    End Sub

    
End Class